package cc.miwuya.community.controller;

import cc.miwuya.common.util.Result;
import cc.miwuya.community.domain.Property;
import cc.miwuya.community.domain.PropertyItem;
import cc.miwuya.community.service.PropertyItemService;
import cc.miwuya.community.service.PropertyService;
import cc.miwuya.core.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 迷雾森林
 * @Date 2024-02-22 20:22
 * @Desc 社区操作物业控制器
 */
@RestController
@RequestMapping("/community/property")
public class PropertyController extends BaseController {
    @Resource
    PropertyService propertyService;
    @Resource
    PropertyItemService propertyItemService;

    /**
     * 获取物业列表
     * @return
     */
    @GetMapping("/list")
    public Result list(String name){
        //propertyService.list(new QueryWrapper<Property>())
        return Result.success(propertyService.getPage(getPage(),name));
    }

    /**
     * 根据ID获取物业信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable Long id){
        Property property = propertyService.getById(id);
        return Result.success(property);
    }

    /**
     * 添加或修改物业信息
     * @param property
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Property property){
        System.out.println(property);
        try {
            propertyService.saveOrUpdate(property);
        } catch (Exception e) {
            return Result.error("操作失败");
        }
        return Result.success();
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        propertyService.removeById(id);
        return Result.success();
    }
    @GetMapping("/listAll")
    public Result listAll(){
        return Result.success(propertyService.list(new QueryWrapper<Property>()));
    }
    /**
     * 获取物业数量
     */
    @GetMapping("/total")
    public Long total(){
        return propertyService.count();
    }

    @PostMapping("/changeProperty")
    public Result changeProperty(@RequestBody PropertyItem propertyItem){
        propertyItemService.remove(new QueryWrapper<PropertyItem>().eq("user_id", propertyItem.getUserId()));
        propertyItemService.save(propertyItem);
        return Result.success();
    }
}
